﻿// -----------------------------------------------------------------------
// <copyright file="AuditLogService.cs" company="AGR">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace AGR.Services.Audit
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using AGR.Model;
    using AGR.Data.Repository;
    using AGR.Data.Repository.Contract;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class AuditLogService : IAuditLogService
    {
        public void LogAudit(AuditLog auditLog)
        {
            if (string.IsNullOrEmpty(auditLog.TableName))
                throw new ArgumentNullException(auditLog.TableName);
            if (string.IsNullOrEmpty(auditLog.ColumnName))
                throw new ArgumentNullException(auditLog.ColumnName);
            if (string.IsNullOrEmpty(auditLog.Action))
                throw new ArgumentNullException(auditLog.Action);
            if (string.IsNullOrEmpty(auditLog.OldValue))
                throw new ArgumentNullException(auditLog.OldValue);
            if (string.IsNullOrEmpty(auditLog.NewValue))
                throw new ArgumentNullException(auditLog.NewValue);
            if (string.IsNullOrEmpty(auditLog.EmployeeCode))
                throw new ArgumentNullException(auditLog.EmployeeCode);

            try
            {
                using (IRepository<AuditLog> auditLogRepository = new AuditLogRepository())
                {
                    auditLogRepository.Add(auditLog);
                    auditLogRepository.SaveChanges();
                }
            }
            catch (Exception)
            {
                throw new Exception("Audit log");
            }
        }

        public void LogAudit(string tableName, string columnName, string action,
                      string oldValue, string newValue, string employee)
        {
            if (string.IsNullOrEmpty(tableName))
                throw new ArgumentNullException(tableName);
            if (string.IsNullOrEmpty(columnName))
                throw new ArgumentNullException(columnName);
            if (string.IsNullOrEmpty(action))
                throw new ArgumentNullException(action);
            if (string.IsNullOrEmpty(oldValue))
                throw new ArgumentNullException(oldValue);
            if (string.IsNullOrEmpty(newValue))
                throw new ArgumentNullException(newValue);
            if (string.IsNullOrEmpty(employee))
                throw new ArgumentNullException(employee);

            try
            {
                using (IRepository<AuditLog> auditLogRepository = new AuditLogRepository())
                {
                    AuditLog auditLog = new AuditLog
                    {
                        TableName = tableName,
                        ColumnName = columnName,
                        Action = action,
                        OldValue = oldValue,
                        NewValue = newValue,
                        EmployeeCode = employee
                    };

                    auditLogRepository.Add(auditLog);
                    auditLogRepository.SaveChanges();
                }
            }
            catch (Exception)
            {
                throw new Exception("Audit log");
            }
        }
    }
}
